package cn.springmvc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.springmvc.dao.BaseDAO;
import cn.springmvc.model.ResultBaseBean;
import cn.springmvc.model.entity.AlbumBean;
import cn.springmvc.service.IAlbumService;

@Service
public class AlbumServiceImpl implements IAlbumService {
	private static final Logger logger = Logger.getLogger(AlbumServiceImpl.class);
	
	@Autowired
	@Qualifier("albumDAOImpl")
	private BaseDAO<AlbumBean> albumDAO;
	
	ResultBaseBean<AlbumBean> resultBaseBean;


	public ResultBaseBean<AlbumBean> insert(AlbumBean t) {
		resultBaseBean = new ResultBaseBean<AlbumBean>();
		logger.info("Prepare to create album with name: " + t.getAlbumName());
		
		AlbumBean queryAlbumBean = new AlbumBean();
		queryAlbumBean.setAlbumName(t.getAlbumName());
		queryAlbumBean.setUserId(t.getUserId());
		List<AlbumBean> AlbumBeanList = albumDAO.select(queryAlbumBean);
		if(AlbumBeanList != null && AlbumBeanList.size() > 0){
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("您已经创建了这个相册");
			logger.info("您已经创建了这个相册");
		}else{	
			boolean result = albumDAO.insert(t);
			
			if(result){
				resultBaseBean.setSuccess(true);
				resultBaseBean.setMessage("创建相册"+t.getAlbumName()+"成功");
				logger.info("创建相册"+t.getAlbumName()+"成功");
			}else{
				resultBaseBean.setSuccess(false);
				resultBaseBean.setMessage("创建相册失败");
				logger.info("创建相册"+t.getAlbumName()+"失败");
			}
		}
		return resultBaseBean;
	}

	public ResultBaseBean<AlbumBean> select(AlbumBean t) {
		resultBaseBean = new ResultBaseBean<AlbumBean>();
		List<AlbumBean> AlbumBeanList = albumDAO.select(t);
		if(AlbumBeanList != null){
			resultBaseBean.setResultList(AlbumBeanList);
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("获取相册列表失败");
		}
		
		return resultBaseBean;
	}

	public ResultBaseBean<AlbumBean> delete(AlbumBean t) {
		resultBaseBean = new ResultBaseBean<AlbumBean>();
		boolean result = albumDAO.delete(t);
		if(result){
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("删除相册失败");
		}
		return resultBaseBean;
	}

	public ResultBaseBean<AlbumBean> update(AlbumBean t) {
		resultBaseBean = new ResultBaseBean<AlbumBean>();
		boolean result = albumDAO.update(t);
		if(result){
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("更新相册失败");
		}
		return resultBaseBean;
	}

	public ResultBaseBean<AlbumBean> selectOne(AlbumBean t) {
		resultBaseBean = new ResultBaseBean<AlbumBean>();
		AlbumBean AlbumBean = albumDAO.selectOne(t);
		if(AlbumBean != null){
			List<AlbumBean> AlbumBeanList = new ArrayList<AlbumBean>();
			AlbumBeanList.add(AlbumBean);
			resultBaseBean.setResultList(AlbumBeanList);
			resultBaseBean.setSuccess(true);
		}else{
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("获取相册失败");
		}
		
		return resultBaseBean;
	}
	
	public ResultBaseBean<AlbumBean> getResultBaseBean() {
		return resultBaseBean;
	}

	public void setResultBaseBean(ResultBaseBean<AlbumBean> resultBaseBean) {
		this.resultBaseBean = resultBaseBean;
	}

}
